package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.pojo.CadreTable;
import com.neuedu.pojo.ClueTable;
import com.neuedu.pojo.PoorFamily;
import com.neuedu.pojo.Result;
import com.neuedu.service.PoorFamilyService;
import com.neuedu.mapper.PoorFamilyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Date;

/**
 * @author Administrator
 * @description 针对表【poor_family(贫困户信息表)】的数据库操作Service实现
 * @createDate 2025-06-17 09:04:08
 */
@Service
public class PoorFamilyServiceImpl extends ServiceImpl<PoorFamilyMapper, PoorFamily>
        implements PoorFamilyService{
    @Autowired
    private PoorFamilyMapper poorFamilyMapper;
    @Override
    public Result<PoorFamily> getPoorFamilyById(Integer id) {
        PoorFamily poorFamily = poorFamilyMapper.selectById(id);
        if (poorFamily !=null){
            return Result.success(poorFamily);
        }
        return Result.fail();
    }

    @Override
    public Result<List<PoorFamily>> getPoorFamily(Integer currentPage, Integer pageSize, String headName, String idCard,String belongArea) {
        // 设置条件
        Page<PoorFamily> page = new Page<>(currentPage,pageSize);
        // 设置条件查询
        LambdaQueryWrapper<PoorFamily> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(headName),PoorFamily::getHeadName,headName)
                .like(StringUtils.isNotEmpty(idCard),PoorFamily::getIdCard,idCard)
                .like(StringUtils.isNotEmpty(belongArea),PoorFamily::getBelongArea,belongArea);
        // 查询
        this.poorFamilyMapper.selectPage(page,wrapper);
        if (page.getRecords().size()>0){
            return Result.success(page.getRecords(),page.getTotal());
        }
        return Result.fail();
    }

    @Override
    public Result<Void> addPoorFamily(PoorFamily poorFamily) {
        // 需要针对表中需要的数据，但是页面添加的时候没有提交的在这里补充
        poorFamily.setIsDelete(0);
        poorFamily.setCreateTime(new Date());
        poorFamily.setUpdateTime(new Date());
        // 添加
        int insert = this.poorFamilyMapper.insert(poorFamily);
        if (insert > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Void> updatePoorFamily(PoorFamily poorFamily) {
        // 需要针对表中需要的数据，但是页面添加的时候没有提交的在这里补充
        // poorFamily.setIsDelete(0);
        // poorFamily.setCreateTime(new Date());
        poorFamily.setUpdateTime(new Date());
        // 添加
        int insert = this.poorFamilyMapper.updateById(poorFamily);
        if (insert > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Void> deletePoorFamily(Integer id) {
        //PoorFamily poorFamily = new PoorFamily();
        //poorFamily.setIsDelete(1);
        //poorFamily.setCadreId(id);
        // 修改
        //int insert = this.poorFamilyMapper.updateById(poorFamily);
        LambdaUpdateWrapper<PoorFamily> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(PoorFamily::getIsDelete,1)
                .eq(PoorFamily::getFamilyId,id);
        int update = this.poorFamilyMapper.update(wrapper);
        if (update > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<PoorFamily> loginPfamily(String number, String password) {
        // 2. 根据number查询数据库
        PoorFamily family=poorFamilyMapper.selectByNumber(number);
        if (family == null) {
            return Result.fail("用户不存在");
        }

        // 3. 比较密码和手机号
        if (password.equals(family.getPassword())) {
            return Result.success(family,"登录成功");
        } else {
            return Result.fail("密码错误");
        }
    }

    @Override
    public Result<Void>updatePassword(String number,String password){
        LambdaUpdateWrapper<PoorFamily>wrapper=new LambdaUpdateWrapper<>();
        wrapper.set(PoorFamily::getUpdateTime,new Date())
                .eq(PoorFamily::getFamilyNumber,number)
                .set(PoorFamily::getPassword,password);
        int update=this.poorFamilyMapper.update(wrapper);
        if(update>0){
            return Result.success();
        }
        return Result.fail();
    }
}




